package cc.rengu.oltp.service.realize.impl;

import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.*;
import cc.rengu.oltp.service.common.dao.impl.*;
import cc.rengu.oltp.service.common.entity.MchntOrderDetail;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.OrderStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 订单服务组件实现
 * Created by 王成 on 2020/3/9
 */
public class OrderServiceImpl implements OrderService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName;

    public OrderServiceImpl() {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
        }
    }

    @Override
    public String generateUserOrderId() throws Exception {
        SequenceMapper sequenceMapper = new SequenceMapperImpl();
        String userOrderSequence = sequenceMapper.getNextSeqNoBySeqName("ORDER_SEQ_NUM", 8);
        String orderIdMiddleValue;
        if (AppParamConstant.DEFAULT_INSTID.length() < 10) {
            String format = "%0" + (10 - AppParamConstant.DEFAULT_INSTID.length()) + "d";
            orderIdMiddleValue = String.format(format, 0) + AppParamConstant.DEFAULT_INSTID;
        } else {
            orderIdMiddleValue = AppParamConstant.DEFAULT_INSTID.substring(0, 10);
        }
        return DateUtil.getCurrentDate() + DateUtil.getCurrentTime() + orderIdMiddleValue + userOrderSequence;
    }

    @Override
    public boolean registerOrderInfo(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, PayOrderInfo payOrderInfo) throws Exception {
        return registerOrderInfo(userOrderInfo, mchntOrderInfoList, null, payOrderInfo);
    }

    @Override
    public boolean registerOrderInfo(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, List<MchntOrderDetail> mchntOrderDetailList, PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        int iReturnCode;
        boolean isSuccess = false;
        /* Step1：开启数据库事务 */
        dbsUtil.dbsBeginTransaction();
        /* Step2：登记用户订单信息 */
        try {
            if (null != userOrderInfo) {
                UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
                /* 用户订单号兼容赋值 */
                if (StringUtil.isNullorEmpty(userOrderInfo.getUserOrderId())) {
                    userOrderInfo.setUserOrderId(generateUserOrderId());
                }
                iReturnCode = userOrderInfoMapper.insertUserOrderInfo(userOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("登记用户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,orderAmt:<{}>,iReturnCode:<{}>",
                            userOrderInfo.getSysSeqNum(), userOrderInfo.getMchntOrderId(), userOrderInfo.getOrderAmt(), iReturnCode);
                    return false;
                }
            }
            /* Step3：登记商户订单信息 */
            if (null != mchntOrderInfoList && !mchntOrderInfoList.isEmpty()) {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
                    iReturnCode = mchntOrderInfoMapper.insertMchntOrderInfo(mchntOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("登记商户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,orderAmt:<{}>,iReturnCode:<{}>",
                                mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getOrderAmt(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step4：登记商户订单明细信息 */
            if (null != mchntOrderDetailList && !mchntOrderDetailList.isEmpty()) {
                MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
                for (MchntOrderDetail mchntOrderDetail : mchntOrderDetailList) {
                    iReturnCode = mchntOrderDetailMapper.insertMchntOrderDetail(mchntOrderDetail);
                    if (0 != iReturnCode) {
                        rglog.error("登记商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                                mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step5：登记支付订单信息 */
            if (null != payOrderInfo) {
                PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
                iReturnCode = payOrderInfoMapper.insertPayOrderInfo(payOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("登记支付订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,channelId:<{}>,orderAmt:<{}>,iReturnCode:<{}>",
                            payOrderInfo.getSysSeqNum(), payOrderInfo.getMchntOrderId(), payOrderInfo.getMsgDstId(), payOrderInfo.getOrderAmt(), iReturnCode);
                    return false;
                }
            }
            isSuccess = true;
        } finally {
            /* Step6：结束数据库事务 */
            dbsUtil.dbsEndTransaction(isSuccess);
        }
        return true;
    }

    @Override
    public boolean updateOrderInfo(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, PayOrderInfo payOrderInfo) throws Exception {
        return updateOrderInfo(userOrderInfo, mchntOrderInfoList, null, payOrderInfo);
    }

    @Override
    public boolean updateOrderInfo(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, List<MchntOrderDetail> mchntOrderDetailList, PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        int iReturnCode;
        boolean isSuccess = false;
        /* Step1：开启数据库事务 */
        dbsUtil.dbsBeginTransaction();
        /* Step2：更新用户订单信息 */
        try {
            if (null != userOrderInfo) {
                UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
                iReturnCode = userOrderInfoMapper.updateUserOrderInfoByPrimaryKey(userOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("更新用户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,transStatus:<{}>,bizStatus:<{}>,iReturnCode:<{}>",
                            userOrderInfo.getSysSeqNum(), userOrderInfo.getMchntOrderId(), userOrderInfo.getTransStatus(), userOrderInfo.getBizStatus(), iReturnCode);
                    return false;
                }
            }
            /* Step3：更新商户订单信息 */
            if (null != mchntOrderInfoList && !mchntOrderInfoList.isEmpty()) {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
                    iReturnCode = mchntOrderInfoMapper.updateMchntOrderInfoByPrimaryKey(mchntOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,mchntFee:<{}>,mchntDiscountFee:<{}>,mchntSettleAmt:<{}>,iReturnCode:<{}>",
                                mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getMchntFee(), mchntOrderInfo.getMchntDiscountFee(), mchntOrderInfo.getMchntSettleAmt(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step4：更新商户订单明细信息 */
            if (null != mchntOrderDetailList && !mchntOrderDetailList.isEmpty()) {
                MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
                for (MchntOrderDetail mchntOrderDetail : mchntOrderDetailList) {
                    iReturnCode = mchntOrderDetailMapper.updateMchntOrderDetailByPrimaryKey(mchntOrderDetail);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                                mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step5：更新支付订单信息 */
            if (null != payOrderInfo) {
                PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
                iReturnCode = payOrderInfoMapper.updatePayOrderInfoByPrimaryKey(payOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("更新支付订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,channelId:<{}>,realPayAmt:<{}>,channelFee:<{}>,settleDate:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>",
                            payOrderInfo.getSysSeqNum(), payOrderInfo.getMchntOrderId(), payOrderInfo.getMsgDstId(), payOrderInfo.getRealPayAmt(), payOrderInfo.getChannelFee(), payOrderInfo.getSettleDate(), payOrderInfo.getChannelSeqNum(), iReturnCode);
                    return false;
                }
            }
            isSuccess = true;
        } finally {
            /* Step6：结束数据库事务 */
            dbsUtil.dbsEndTransaction(isSuccess);
        }
        return true;
    }

    @Override
    public boolean updateOrderInfo(OrderInfo orderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        int iReturnCode;
        boolean isSuccess = false;
        /* Step1：开启数据库事务 */
        dbsUtil.dbsBeginTransaction();
        try {
            /* Step2：更新用户订单信息 */
            if (null != orderInfo.getUserOrderInfo()) {
                UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
                iReturnCode = userOrderInfoMapper.updateUserOrderInfoByPrimaryKey(orderInfo.getUserOrderInfo());
                if (0 != iReturnCode) {
                    rglog.error("更新用户订单信息失败,sysSeqNum:<{}>,userOrderId:<{}>,transStatus:<{}>,bizStatus:<{}>,iReturnCode:<{}>",
                            orderInfo.getUserOrderInfo().getSysSeqNum(), orderInfo.getUserOrderInfo().getUserOrderId(), orderInfo.getUserOrderInfo().getTransStatus(), orderInfo.getUserOrderInfo().getBizStatus(), iReturnCode);
                    return false;
                }
            }
            /* Step3：更新商户订单信息 */
            if (null != orderInfo.getMchntOrderInfoList() && !orderInfo.getMchntOrderInfoList().isEmpty()) {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                for (MchntOrderInfo mchntOrderInfo : orderInfo.getMchntOrderInfoList()) {
                    iReturnCode = mchntOrderInfoMapper.updateMchntOrderInfoByPrimaryKey(mchntOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,mchntFee:<{}>,mchntDiscountFee:<{}>,mchntSettleAmt:<{}>,iReturnCode:<{}>",
                                mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getMchntFee(), mchntOrderInfo.getMchntDiscountFee(), mchntOrderInfo.getMchntSettleAmt(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step4：更新商户订单明细信息 */
            if (null != orderInfo.getMchntOrderDetailList() && !orderInfo.getMchntOrderDetailList().isEmpty()) {
                MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
                for (MchntOrderDetail mchntOrderDetail : orderInfo.getMchntOrderDetailList()) {
                    iReturnCode = mchntOrderDetailMapper.updateMchntOrderDetailByPrimaryKey(mchntOrderDetail);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                                mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step5：更新支付订单信息 */
            if (null != orderInfo.getPayOrderInfoList() && !orderInfo.getPayOrderInfoList().isEmpty()) {
                PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
                for (PayOrderInfo payOrderInfo : orderInfo.getPayOrderInfoList()) {
                    iReturnCode = payOrderInfoMapper.updatePayOrderInfoByPrimaryKey(payOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("更新支付订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,channelId:<{}>,realPayAmt:<{}>,channelFee:<{}>,settleDate:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>",
                                payOrderInfo.getSysSeqNum(), payOrderInfo.getMchntOrderId(), payOrderInfo.getMsgDstId(), payOrderInfo.getRealPayAmt(), payOrderInfo.getChannelFee(), payOrderInfo.getSettleDate(), payOrderInfo.getChannelSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            isSuccess = true;
        } finally {
            /* Step6：结束数据库事务 */
            dbsUtil.dbsEndTransaction(isSuccess);
        }
        return true;
    }

    @Override
    public boolean updateOrderInfo(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList) throws Exception {
        return updateOrderInfoByOrderId(userOrderInfo,mchntOrderInfoList,null);
    }

    @Override
    public boolean updateOrderInfoByOrderId(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, List<MchntOrderDetail> mchntOrderDetailList) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        int iReturnCode;
        boolean isSuccess = false;
        /* Step1：开启数据库事务 */
        dbsUtil.dbsBeginTransaction();
        try {
            /* Step2：更新用户订单信息 */
            if (null != userOrderInfo) {
                UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
                iReturnCode = userOrderInfoMapper.updateUserOrderInfoByUserOrderId(userOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("更新用户订单信息失败,sysSeqNum:<{}>,userOrderId:<{}>,transStatus:<{}>,bizStatus:<{}>,iReturnCode:<{}>",
                            userOrderInfo.getSysSeqNum(), userOrderInfo.getUserOrderId(), userOrderInfo.getTransStatus(), userOrderInfo.getBizStatus(), iReturnCode);
                    return false;
                }
            }
            /* Step3：更新商户订单信息 */
            if (null != mchntOrderInfoList && !mchntOrderInfoList.isEmpty()) {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
                    iReturnCode = mchntOrderInfoMapper.updateMchntOrderInfoByMchntOrderId(mchntOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,mchntFee:<{}>,mchntDiscountFee:<{}>,mchntSettleAmt:<{}>,iReturnCode:<{}>",
                                mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getMchntFee(), mchntOrderInfo.getMchntDiscountFee(), mchntOrderInfo.getMchntSettleAmt(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step4：更新商户订单明细信息 */
            if (null != mchntOrderDetailList && !mchntOrderDetailList.isEmpty()) {
                MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
                for (MchntOrderDetail mchntOrderDetail : mchntOrderDetailList) {
                    iReturnCode = mchntOrderDetailMapper.updateMchntOrderDetailByPrimaryKey(mchntOrderDetail);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                                mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            isSuccess = true;
        } finally {
            /* Step4：结束数据库事务 */
            dbsUtil.dbsEndTransaction(isSuccess);
        }
        return true;
    }

    @Override
    public boolean updateOrderStatus(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, PayOrderInfo payOrderInfo) throws Exception {
        return updateOrderStatus(userOrderInfo, mchntOrderInfoList, null, payOrderInfo);
    }

    @Override
    public boolean updateOrderStatus(UserOrderInfo userOrderInfo, List<MchntOrderInfo> mchntOrderInfoList, List<MchntOrderDetail> mchntOrderDetailList, PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        int iReturnCode;
        boolean isSuccess = false;
        /* Step1：开启数据库事务 */
        dbsUtil.dbsBeginTransaction();
        /* Step2：更新用户订单信息 */
        try {
            if (null != userOrderInfo) {
                UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
                iReturnCode = userOrderInfoMapper.updateUserOrderInfoByPrimaryKey(userOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("更新用户订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,transStatus:<{}>,bizStatus:<{}>,iReturnCode:<{}>",
                            userOrderInfo.getSysSeqNum(), userOrderInfo.getMchntOrderId(), userOrderInfo.getTransStatus(), userOrderInfo.getBizStatus(), iReturnCode);
                    return false;
                }
            }
            /* Step3：更新商户订单信息 */
            if (null != mchntOrderInfoList && !mchntOrderInfoList.isEmpty()) {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
                    iReturnCode = mchntOrderInfoMapper.updateMchntOrderStatusByPrimaryKey(mchntOrderInfo);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单状态失败,sysSeqNum:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,iReturnCode:<{}>",
                                mchntOrderInfo.getSysSeqNum(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step4：更新商户订单明细信息 */
            if (null != mchntOrderDetailList && !mchntOrderDetailList.isEmpty()) {
                MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
                for (MchntOrderDetail mchntOrderDetail : mchntOrderDetailList) {
                    iReturnCode = mchntOrderDetailMapper.updateMchntOrderDetailByPrimaryKey(mchntOrderDetail);
                    if (0 != iReturnCode) {
                        rglog.error("更新商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                                mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
                        return false;
                    }
                }
            }
            /* Step5：更新支付订单信息 */
            if (null != payOrderInfo) {
                PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
                iReturnCode = payOrderInfoMapper.updatePayOrderInfoByPrimaryKey(payOrderInfo);
                if (0 != iReturnCode) {
                    rglog.error("更新支付订单信息失败,sysSeqNum:<{}>,mchntOrderId:<{}>,channelId:<{}>,realPayAmt:<{}>,channelFee:<{}>,settleDate:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>",
                            payOrderInfo.getSysSeqNum(), payOrderInfo.getMchntOrderId(), payOrderInfo.getMsgDstId(), payOrderInfo.getRealPayAmt(), payOrderInfo.getChannelFee(), payOrderInfo.getSettleDate(), payOrderInfo.getChannelSeqNum(), iReturnCode);
                    return false;
                }
            }
            isSuccess = true;
        } finally {
            /* Step6：结束数据库事务 */
            dbsUtil.dbsEndTransaction(isSuccess);
        }
        return true;
    }

    @Override
    public OrderInfo selectOrderInfoByUserOrderId(String instId, String userOrderId) throws Exception {
        /* Step1：查询用户订单信息 */
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByUserOrderId(instId, userOrderId);
        if (null != userOrderInfo) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setUserOrderInfo(userOrderInfo);
            /* Step1.1：订单状态校验 */
            if (OrderStatusEnum.CLOSE.getStatus().equals(userOrderInfo.getOrderStatus())) {
                rglog.info("用户订单已关闭,不允许再次支付,instId:<{}>,userOrderId:<{}>,orderStatus:<{}>", instId, userOrderId, userOrderInfo.getOrderStatus());
                throw new BizException(OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespCode(), OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespDesc());
            } else if (OrderStatusEnum.INVALID.getStatus().equals(userOrderInfo.getOrderStatus())) {
                rglog.info("用户订单已过期,不允许再次支付,instId:<{}>,userOrderId:<{}>,orderStatus:<{}>", instId, userOrderId, userOrderInfo.getOrderStatus());
                throw new BizException(OltpRpcdEnum.ORDER_EXPIRED_ERROR.getRespCode(), OltpRpcdEnum.ORDER_EXPIRED_ERROR.getRespDesc());
            } else if (OrderStatusEnum.FINISH.getStatus().equals(userOrderInfo.getOrderStatus())) {
                rglog.info("用户订单已完成,不允许再次支付,instId:<{}>,userOrderId:<{}>,orderStatus:<{}>", instId, userOrderId, userOrderInfo.getOrderStatus());
                throw new BizException(OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespCode(), OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespDesc());
            } else if (BizStatusEnum.PROCESSING.getStatus().equals(userOrderInfo.getBizStatus())) {
                rglog.info("用户订单处理处理中,不允许再次支付,instId:<{}>,userOrderId:<{}>,orderStatus:<{}>", instId, userOrderId, userOrderInfo.getOrderStatus());
                throw new BizException(OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespCode(), OltpRpcdEnum.ORDER_STATUS_ABNORMAL_ERROR.getRespDesc());
            }
            /* Step1.2：订单有效期校验 */
            String curDateTime = DateUtil.getCurrentDateTime("yyyyMMddHHmmss");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date beginDate = simpleDateFormat.parse(curDateTime);
            Date endDate = simpleDateFormat.parse(userOrderInfo.getOrderExpiryTime());
            if (beginDate.compareTo(endDate) > 0) {
                rglog.info("订单已经过了有效期，当前日期curDateTime:<{}>,订单有效期orderExpiryTime:<{}>", curDateTime, userOrderInfo.getOrderExpiryTime());
                userOrderInfo.setOrderStatus(OrderStatusEnum.INVALID.getStatus());
                userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                if (!updateOrderInfo(userOrderInfo, null, null)) {
                    rglog.error("更新用户订单/商户订单信息失败");
                }
                throw new BizException(OltpRpcdEnum.ORDER_EXPIRED_ERROR.getRespCode(), OltpRpcdEnum.ORDER_EXPIRED_ERROR.getRespDesc());
            }
            /* Step2：查询商户订单信息 */
            MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
            List<MchntOrderInfo> mchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(instId, userOrderInfo.getSysSeqNum());
            orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
            /* Step3：查询商户订单明细信息 */
            MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
            List<MchntOrderDetail> mchntOrderDetailList = mchntOrderDetailMapper.selectMchntOrderDetailBySysSeqNum(instId, userOrderInfo.getSysSeqNum());
            orderInfo.setMchntOrderDetailList(mchntOrderDetailList);
            return orderInfo;
        }
        return null;
    }

    @Override
    public OrderInfo selectOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        /* Step1：查询用户订单信息 */
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, sysSeqNum);
        if (null != userOrderInfo) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setUserOrderInfo(userOrderInfo);
            /* Step2：查询商户订单信息 */
            MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
            List<MchntOrderInfo> mchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(instId, userOrderInfo.getSysSeqNum());
            orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
            /* Step3：查询商户订单明细信息 */
            MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
            List<MchntOrderDetail> mchntOrderDetailList = mchntOrderDetailMapper.selectMchntOrderDetailBySysSeqNum(instId, userOrderInfo.getSysSeqNum());
            orderInfo.setMchntOrderDetailList(mchntOrderDetailList);
            /* Step4：查询支付订单信息 */
            PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
            List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, userOrderInfo.getSysSeqNum());
            orderInfo.setPayOrderInfoList(payOrderInfoList);
            return orderInfo;
        }
        return null;
    }
}
